package com.didispace.chapter36.controller;

import com.didispace.chapter36.entity.User;
import com.didispace.chapter36.mapper.UserMapper;
import com.didispace.chapter36.service.IUserService;
import com.didispace.chapter36.struct.UserStructMapper;
import com.didispace.chapter36.vo.UserPageVO;
import com.didispace.chapter36.vo.UserVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * mapstruct实例控制器
 *
 * @Author Liurb
 * @Date 2022/11/13
 */
@RestController
@RequestMapping("/demo_api/mapstruct")
public class MapStructController {

    @Resource
    UserMapper userMapper;

    @GetMapping("/home/{id}")
    public UserVO home(@PathVariable("id")Integer id) {
        User student = userMapper.findById(2L);
        UserVO studentHomeVo = UserStructMapper.INSTANCES.toUserVO(student);
        return studentHomeVo;
    }

    @GetMapping("/page")
    public List<UserPageVO> page() {
        List<User> userList = userMapper.findByName(null);
        List<UserPageVO> userPageVOList = userList.stream().map(item -> {
            UserPageVO studentPageVo = UserStructMapper.INSTANCES.toUserPageVO(item);
            return studentPageVo;
        }).collect(Collectors.toList());

        return userPageVOList;
    }

}
